<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\AuthContent;
use App\Admin\Actions\Deleted;
use App\Models\Content;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Position;

class ContentController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '信息管理';

    private function __($key)
    {
        return [
            'Id' => 'ID',
            'Title' => '标题',
            'UserName' => '发布者',
            'Position' => '职位',
            'Type' => '类型',
            'Mobile' => '联系方式',
            'Is charge' => '是否收费',
            'Descrition' => '描述',
            'Images' => '图片',
            'Tags' => '标签',
            'Address' => '地址',
            'Status' => '状态',
            'Refresh at' => '刷新时间',
            'Created at' => '创建时间',
            'Updated at' => '修改时间',
        ][$key] ?? $key;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Content());
        $adminUser = Auth::guard('admin')->user();
        $grid->model()->where('status', '>=', 0)
            ->orderBy('id', 'desc');
        $typeStrArr = array_flip(Content::TYPE);

        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableColumnSelector();
        $grid->disableRowSelector();

        $grid->column('id', $this->__('Id'));
        $grid->column('images', $this->__('Images'))->display(function($images, $column) {
            if (!empty($images)) {
                return $column->carousel(80, 80, '/storage');
            } else {
                return '';
            }
        });
        $grid->column('title', $this->__('Title'))->filter('like');
        $grid->column('user_id', $this->__('UserName'))->display(function($userId) {
            if ($userId == 0) {
                return '系统';
            } else {
                return User::where('id', $userId)->value('nickname') ?: '--';
            }
        });

        $grid->column('type', $this->__('Type'))->using($typeStrArr)->filter($typeStrArr);

        $grid->column('mobile', $this->__('Mobile'))->filter();
        // $grid->column('type', $this->__('Type'));
        $grid->column('is_charge', $this->__('Is charge'))->icon([
            0 => 'toggle-off',
            1 => 'toggle-on',
        ])->filter(Content::IS_CHARGE);
        // $grid->column('descrition', $this->__('Descrition'));
        // $grid->column('stars', $this->__('Stars'));
        $grid->column('tags', $this->__('Tags'))->label()->filter('like');
        $grid->column('address', $this->__('Address'));
        // $grid->column('longitude', $this->__('Longitude'));
        // $grid->column('latitude', $this->__('Latitude'));
        // $grid->column('status', $this->__('Status'))->using(Content::STATUS)->filter(Content::STATUS);
        $grid->column('refresh_at', $this->__('Refresh at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        $grid->column('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        $grid->column('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        })->sortable();
        // $grid->column('data', $this->__('Data'));

        $grid->actions(function ($actions) {
            $actions->add(new Deleted);
            $actions->disableDelete();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $item = Content::findOrFail($id);
        $show = new Show($item);
        $typeStrArr = array_flip(Content::TYPE);

        $show->field('id', $this->__('Id'));
        $show->field('user_id',  $this->__('UserName'))->as(function($userId) {
            if ($userId == 0) {
                return '系统';
            } else {
                return User::where('id', $userId)->value('nickname') ?: '--';
            }
        });
        if (!empty($item->position_id)) {
            $show->field('position_id', $this->__('Position'))->as(function($positionId) {
                if ($positionId == 0) {
                    return '--';
                } else {
                    return Position::where('id', $positionId)->value('name') ?: '--';
                }
            });
        }
        $show->field('type', $this->__('Type'))->using($typeStrArr);
        $show->field('mobile', $this->__('Mobile'));
        // $show->field('type', $this->__('Type'));
        $show->field('title', $this->__('Title'));
        $show->field('descrition', $this->__('Descrition'))->unescape();
        $show->field('images', $this->__('Images'))->carousel(300, 200, '/storage/');
        // $show->field('stars', $this->__('Stars'));
        $show->field('tags', $this->__('Tags'))->label();
        $show->field('address', $this->__('Address'));
        // $show->field('longitude', $this->__('Longitude'));
        // $show->field('latitude', $this->__('Latitude'));
        // $show->field('status', $this->__('Status'))->using(Content::STATUS);
        $show->field('refresh_at', $this->__('Refresh at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('created_at', $this->__('Created at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        $show->field('updated_at', $this->__('Updated at'))->display(function($created) {
            return date('Y-m-d H:i:s', strtotime($created));
        });
        // $show->field('data', $this->__('Data'));
        $show->field('is_charge', $this->__('Is charge'))->using(Content::IS_CHARGE);

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Content());
        $id = request()->route()->parameter('content'); 
        $model = $form->model()->find($id);

        $typeStrArr = array_flip(Content::TYPE);
        $positionList = Position::pluck('name', 'id')->toArray();
        $form->hidden('user_id')->default($model->user_id ?? 0);
        
        if ($form->isEditing()) {
            $form->hidden('type', $model->type);
            $form->display('type', $this->__('Type'))->with(function ($value) use ($typeStrArr) {
                return $typeStrArr[$value] ?? '--';
            });
            if ($model->type == 4) {
                $form->select('position_id', $this->__('Position'))->options($positionList)->rules('required');
            }
        } else {
            $form->select('type', $this->__('Type'))->options($typeStrArr)->rules('required')->when(4, function (Form $form) use ($positionList) {
                $form->select('position_id', $this->__('Position'))->options($positionList)->rules('required');
            });
        }


        $form->mobile('mobile', $this->__('Mobile'))->rules('required');
        $form->text('title', $this->__('Title'))->rules('required');
        $form->simditor('descrition', $this->__('Descrition'));
        $form->multipleImage('images', $this->__('Images'))->removable();
        // $form->decimal('stars', $this->__('Stars'))->default(0.00);
        $form->tags('tags', $this->__('Tags'));

        $form->text('address', $this->__('Address'));
        // $form->decimal('longitude', $this->__('Longitude'))->default(0.00000000);
        // $form->decimal('latitude', $this->__('Latitude'))->default(0.00000000);
        // dd($form->model()->longitude);
        $form->latlong('latitude', 'longitude', 'Position')->zoom(16)->default(['lat' => $model->latitude ?? 20.02496791, 'lng' => $model->longitude ?? 110.16174674]);
        // $form->radio('status', $this->__('Status'))->options(Content::STATUS)->default($model->status);
        // $form->text('data', $this->__('Data'));
        $states = [
            'on'  => ['value' => 1, 'text' => '收费', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '免费', 'color' => 'danger'],
        ];
        $form->switch('is_charge', $this->__('Is charge'))->default($model->is_charge ?? 0)->states($states);

        $form->footer(function ($footer) {


            // 去掉`提交`按钮
            // $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
